using System;
using Impact.Model.Objects;
using Impact.Views;
using Impact.Model.Service;
using StructureMap;

namespace Impact.Controllers
{
    /// <summary>
    /// Controller for manipulating Profile objects
    /// </summary>
    public class ProfileController
    {
        private IProfileEditorView view;
        private IProfileService service;
        private Guid userId;


        /// <summary>
        /// default constructor
        /// </summary>
        /// <param name="view">the view this controller is using</param>
        /// <param name="userId">user ID of administrator using this functionality</param>
        public ProfileController( IProfileEditorView view, Guid userId )
        {
            this.view = view;
            this.userId = userId;
            this.service = ObjectFactory.GetInstance<IProfileService>();
        }

        /// <summary>
        /// perform startup functionality for the controller
        /// </summary>
        /// <param name="isFirstTime">true if this is the first time this controller is loaded,
        /// false if this is a postback</param>
        public void InitializeEditor( bool isFirstTime )
        {

            if ( isFirstTime )
            {
                Profile profile = null;
                if ( CurrentProfileId != null )
                {
                    // if profileId is valid, we're editing.
                    profile = service.FetchProfile( CurrentProfileId.Value );
                }
                else
                {
                    // otherwise, we're adding.
                    profile = new Profile();
                }
                view.PopulateForm( profile );
            }
        }


        /// <summary>
        /// gets the profileId of the page currently being edited.
        /// </summary>
        protected int? CurrentProfileId
        {
            get
            {
                string profileIdParam = view.Parameters["pid"];
                int profileId = -1;
                int? output = null;
                if ( Int32.TryParse( profileIdParam, out profileId ) )
                {
                    output = profileId;
                }
                return output;
            }
        }

        /// <summary>
        /// commit the data from the view to the data store
        /// </summary>
        public void SaveProfile()
        {
            Profile profile = view.RetrieveFormData();

            float latitude, longitude;
            string location;
            LocationController.GetLocationFromZip( profile.Zipcode, out latitude, out longitude, out location );

            profile.Latitude = latitude;
            profile.Longitude = longitude;
            //profile.Location = location;

            service.Save( profile, userId );

        }


        /// <summary>
        /// returns the URL for the Profile given
        /// </summary>
        public static string GetUrl( Profile profile )
        {
            return ContentPresenter.FormatUrl( profile );
        }


        /// <summary>
        /// Toggle the Enabled flag for the given Profile
        /// </summary>
        public static void ToggleEnabled( int profileId, Guid userId )
        {
            IProfileService service = ObjectFactory.GetInstance<IProfileService>();

            Profile profile = service.FetchProfile( profileId );
            profile.Enabled = !profile.Enabled;

            service.Save( profile, userId );

            //decimal latitude, longitude;
            //string location;

            //ProfileCollection profiles = service.FetchProfiles( false );
            //foreach ( Profile profile in profiles )
            //{
            //    LocationController.GetLocationFromZip( profile.Zipcode, out latitude, out longitude, out location );

            //    profile.Latitude = latitude;
            //    profile.Longitude = longitude;

            //    service.Save( profile, userId );
            //}

        }

        /// <summary>
        /// Delete the given profile
        /// </summary>
        /// <param name="profileId"></param>
        public static void DeleteProfile( int profileId )
        {
            IProfileService service = ObjectFactory.GetInstance<IProfileService>();
            service.Remove( profileId );
        }

        /// <summary>
        /// retrieve all profiles
        /// </summary>
        /// <returns></returns>
        public static ProfileCollection GetProfiles()
        {
            IProfileService service = ObjectFactory.GetInstance<IProfileService>();
            return service.FetchProfiles( false );
        }

        /// <summary>
        /// retrieve the given profile
        /// </summary>
        /// <param name="profileId"></param>
        /// <returns></returns>
        public static Profile GetProfile( int profileId )
        {
            IProfileService service = ObjectFactory.GetInstance<IProfileService>();
            return service.FetchProfile( profileId );
        }

    }
}
